<%@page import="qldc.pm.tiensu.model.TienSu"%>
<%@page import="qldc.pm.tiensu.dto.DS_NguoiTienSuDTO"%>
<%@page import="qldc.pm.tiensu.searchcontainer.TienSuCanXoaAnTichSearch"%>
<%@page import="qldc.pm.tiensu.searchcontainer.TienSuCanXoaAnTichSearchTerms"%>
<%@page import="qldc.pm.tiensu.searchcontainer.TienSuCanXoaAnTichDisplayTerms"%>
<%@page import="com.liferay.portal.kernel.dao.search.RowChecker"%>
<%@page import="qldc.pm.tiensu.constants.TrangThaiTienSuEnum"%>
<%@page import="java.text.Format"%>
<%@page import="qldc.pm.tiensu.constants.TrangThaiTienSuTrongThongKeEnum"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="com.liferay.portal.kernel.util.Constants"%>
<%@page import="javax.portlet.ActionResponse"%>
<%@page import="com.liferay.portlet.PortletURLUtil"%>
<%@page import="qldc.pm.qtht.dmdc.model.Tinh"%>
<%@page import="qldc.pm.qtht.dmdc.service.TinhLocalServiceUtil"%>
<%@page import="java.util.List"%>
<%@page import="qldc.pm.tiensu.service.DoiTuongTienSuLocalServiceUtil"%>
<%@page import="java.util.Date"%>
<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@page import="java.text.SimpleDateFormat"%>

<%@include file="../../init.jsp"%>

<%
	PortletURL portletURL = renderResponse.createRenderURL();
	portletURL.setParameter("mvcPath", "/html/view.jsp");
	portletURL.setParameter("chonChucNangItem",	TienSuConstants.DS_BKTT);
	portletURL.setParameter("loaiBKTK",TienSuConstants.DS_CAN_XOA_AN_TICH);
	portletURL.setParameter("tabs1",TienSuConstants.DS_CAN_XOA_AN_TICH);

	PortletURL thoatUrl =  renderResponse.createRenderURL();
	
	thoatUrl.setParameter("mvcPath", "/html/view.jsp");
	thoatUrl.setParameter("chonChucNangItem",	TienSuConstants.BAO_CAO_THONG_KE);
	
	String pathThemeImages = themeDisplay.getPathThemeImages();
	SimpleDateFormat dateFormat = new SimpleDateFormat(TienSuConstants.DATE_PATTERN);
	
%>

<portlet:resourceURL var="loadQuanHuyen" id="loadQuanHuyen">
	<portlet:param name="tinhIdSelected" value="{0}" />
</portlet:resourceURL>

<portlet:resourceURL var="loadPhuongXa" id="loadPhuongXa">
	<portlet:param name="quanHuyenIdSelected" value="{0}" />
</portlet:resourceURL>

<script type="text/javascript">
var NhapNgay = (function() {
	function NhapNgay() {}
	/*
	 * 
	 */
	 NhapNgay.prototype.initCalendars = function() {
			$("#<portlet:namespace/>tuNgayToaAn").datepicker({
				showOn: "both",
				buttonImage: "<%=pathThemeImages%>/common/calendar.png",
					buttonImageOnly : true,
					buttonText : "<liferay-ui:message key="tuNgayPhamToi" />",
					dateFormat : "dd/mm/yy",
					onClose: function( selectedDate ) {
						 $("#<portlet:namespace/>denNgayToaAn").datepicker( "option", "minDate", selectedDate );
						 }
				});
			$("#<portlet:namespace/>denNgayToaAn").datepicker({
				showOn: "both",
				buttonImage: "<%=pathThemeImages%>/common/calendar.png",
					buttonImageOnly : true,
					buttonText : "<liferay-ui:message key="denNgayPhamToi" />",
					dateFormat : "dd/mm/yy",
					onClose: function( selectedDate ) {
						 $("#<portlet:namespace/>tuNgayToaAn").datepicker( "option", "maxDate", selectedDate );
						 }
				});

		};

		return NhapNgay;
	})();

	$(function() {
		var dom = new NhapNgay();

		dom.initCalendars();
		$('#tree-xoa-an-tich').checktree();
		
		$('#checkAllTienSu').change(function() {
		    if($(this).is(":checked")) {
		    	$('input:checkbox[name=<portlet:namespace/>soDinhDanhCaNhanList]').prop('checked', true);
		    	$('input:checkbox[name=<portlet:namespace/>tienSuIdList]').prop('checked', true);
		    }else {

		    	$('input:checkbox[name=<portlet:namespace/>soDinhDanhCaNhanList]').prop('checked', false);
		    	$('input:checkbox[name=<portlet:namespace/>tienSuIdList]').prop('checked', false);;
		    }
		        
		});

	});
	

	function checkButtonCheckAll(){
		var selectList = $('[name="<portlet:namespace/>soDinhDanhCaNhanList"]');
		var selectedSDDCN = getSelectedItem(selectList);
		selectList = $('[name="<portlet:namespace/>tienSuIdList"]');
		var selectedTienSu = getSelectedItem(selectList);
		if (selectedSDDCN.length == 0 && selectedTienSu.length == 0 ){
			$('#checkAllTienSu').prop('checked', false);
		} else {
			$('#checkAllTienSu').prop('checked', true);
		}
	}
</script>

<portlet:actionURL var="inDanhSachNguoiCoTienSuCanXoaAnTichURL">
	<portlet:param name="<%=TienSuConstants.ACTION_HANDLER_PARAM %>" value="<%=TienSuConstants.REPORT_ACTION %>"/>
	<portlet:param name="<%=Constants.ACTION %>" value="inDanhSachNguoiCoTienSuCanXoaAnTich"/>
	<portlet:param name="redirect" value="<%=portletURL.toString() %>"/>
</portlet:actionURL>

<fieldset>
	

<div  class="main-Content" >
	<form action="<%=inDanhSachNguoiCoTienSuCanXoaAnTichURL.toString()%>" id="<portlet:namespace/>frm" method="POST" target="_blank">
			<input type="hidden" name="<portlet:namespace />cmd" id="<portlet:namespace />cmd">
			<input type="hidden" name="<portlet:namespace/>soDinhDanhCaNhans" id="<portlet:namespace/>soDinhDanhCaNhans">
			<input type="hidden" name="<portlet:namespace/>tienSuIds" id="<portlet:namespace/>tienSuIds">

		
		<%
			RowChecker rowChecker = new RowChecker(renderResponse);

			SimpleDateFormat sdf = new SimpleDateFormat(TienSuConstants.DATE_PATTERN);
			TienSuCanXoaAnTichSearch dsTienSuSearch = new TienSuCanXoaAnTichSearch(renderRequest,
					portletURL);
			TienSuCanXoaAnTichDisplayTerms displayTerms = (TienSuCanXoaAnTichDisplayTerms) dsTienSuSearch
					.getDisplayTerms();
			TienSuCanXoaAnTichSearchTerms searchTerms = (TienSuCanXoaAnTichSearchTerms) dsTienSuSearch
					.getSearchTerms();
			
			long noiThuongTruTinhIdSelected = searchTerms.getTinhId();
			long noiThuongTruQuanHuyenIdSelected = searchTerms.getQuanHuyenId();
			long noiThuongTruPhuongXaIdSelected = searchTerms.getPhuongXaId();
			int totalRow = DoiTuongTienSuLocalServiceUtil.countDsTienSuByVungDiaLy_ThoiGianToaAn
					(searchTerms.getTinhId(), searchTerms.getQuanHuyenId(),searchTerms.getPhuongXaId(),
							searchTerms.getTuNgayToaAn(),
							searchTerms.getDenNgayToaAn(),TrangThaiTienSuEnum.DANG_THI_HANH_AN.getValue());
		
		%>
		<table class="gridnormal" style="padding-top: 10px;padding-left: 10px">			
			<tr>
				<td class="aui-w20"><liferay-ui:message key="vung-dia-ly" /></td>

				<td colspan="3" class="aui-w80" ><select class="display-inline" style="width: 30%" name="<portlet:namespace/><%=TienSuCanXoaAnTichDisplayTerms.TINH_ID %>" id="<portlet:namespace/>noiThuongTruTinhId"
					onchange="loadQuanHuyen('<portlet:namespace />noiThuongTruTinhId', '<portlet:namespace />noiThuongTruQuanHuyenId', '<portlet:namespace />noiThuongTruPhuongXaId', '<%=loadQuanHuyen.toString()%>','<%=noiThuongTruQuanHuyenIdSelected%>');">
						<option value="0"><liferay-ui:message key="chon-tinh" /></option>
						<%
							List<Tinh> tinhs = TinhLocalServiceUtil.findAll();

							for (Tinh tinh : tinhs) {
						%>
						<option value="<%=tinh.getTinhId()%>" <%=(noiThuongTruTinhIdSelected == tinh.getTinhId()|| tinh.getLaMacDinh()) ? "selected=selected"
						: StringPool.BLANK%>><%=tinh.getTenTinh()%></option>
						<%
							}
						%>
				</select>
				 <select class="display-inline" style="width: 30%" name="<portlet:namespace/><%=TienSuCanXoaAnTichDisplayTerms.QUAN_HUYEN_ID %>" id="<portlet:namespace/>noiThuongTruQuanHuyenId" onchange="loadPhuongXa('<portlet:namespace/>noiThuongTruQuanHuyenId', '<portlet:namespace/>noiThuongTruPhuongXaId', '<%=loadPhuongXa.toString()%>', '<%=noiThuongTruPhuongXaIdSelected%>');">
				 	<option value="0"><liferay-ui:message key="chon-quan-huyen" /></option>
				 
				 </select>

				<select class="display-inline" style="width: 30%" name="<portlet:namespace/><%=TienSuCanXoaAnTichDisplayTerms.PHUONG_XA_ID %>" id="<portlet:namespace/>noiThuongTruPhuongXaId">
						<option value="0"><liferay-ui:message key="chon-phuong-xa" /></option>				
				</select>
				
				</td>
				
			</tr>
			<tr>
				<td class="txtlabel aui-w20"><liferay-ui:message key="ngay-toa-an-tu" /></td>
				<td class="aui-w40 ip-datepicker"><input type="text" id="<portlet:namespace/>tuNgayToaAn" name="<portlet:namespace/><%=TienSuCanXoaAnTichDisplayTerms.TU_NGAY_TOA_AN %>" value="<%=Validator.isNotNull(displayTerms.getTuNgayToaAn())? sdf.format(displayTerms.getTuNgayToaAn()) : ""%>" />
				&nbsp;&nbsp;<liferay-ui:message key="den-ngay" />
				&nbsp;&nbsp;<input type="text" id="<portlet:namespace/>denNgayToaAn" name="<portlet:namespace/><%=TienSuCanXoaAnTichDisplayTerms.DEN_NGAY_TOA_AN %>" value="<%=Validator.isNotNull(displayTerms.getDenNgayToaAn())?sdf.format(displayTerms.getDenNgayToaAn()) : ""%>" />
				</td>
				<td colspan="2"></td>
			</tr>
			<tr>
			<td></td>
			<td colspan="3">
			<div align="left">
				<input type=button onclick="timDanhSach();"  id="<portlet:namespace/>Tim"  class="btn btn-primary" name="<portlet:namespace/>Tim" value="<liferay-ui:message key="tim-kiem" />" /> 
			
				<input type=button onclick="taoBaoCao();" <%=totalRow == 0?"disabled='disabled'":"" %>  id="<portlet:namespace/>BaoCao"  class="btn btn-primary" name="<portlet:namespace/>BaoCao" value="<liferay-ui:message key="xuat-bao-cao" />" /> 
			
				<input type=button class="btn"  name="<portlet:namespace/>thoat" value="<liferay-ui:message key="tro-ve" />"  onclick="window.location = '<%=thoatUrl.toString() %>';" />
			</div>
			</td>
			
			</tr>
		</table>
	</form>

<div class="ds-can-xoa-an-tich">
<%
if (totalRow > 0){
%>
<div class="header-line">
				<div class="main-item aui-w3 txt-Bold"><input type="checkbox" id="checkAllTienSu" /></div>
				<div class="main-item aui-w20 txt-Bold" ><liferay-ui:message key="so-dinh-danh-ca-nhan" /></div>
				<div class="main-item aui-w20 txt-Bold"><liferay-ui:message key="ho-va-ten" /></div>
				<div class="main-item aui-w12 txt-Bold" ><liferay-ui:message key="ngay-sinh" /></div>
				<div class="main-item aui-w45 txt-Bold"><liferay-ui:message key="dia-chi-thuong-tru" /></div>
			</div>
<%} else if (searchTerms.getTinhId() > 0){
	%>
	<div class="alert alert-info"><liferay-ui:message key="no-web-content-were-found" /></div>
<% }%>
<ul id="tree-xoa-an-tich">
	
	

<%
	if (searchTerms.getTinhId() > 0){
		List<DS_NguoiTienSuDTO> canXoaAnTichs = DoiTuongTienSuLocalServiceUtil.findDsTienSuByVungDiaLy_ThoiGianToaAn
				(searchTerms.getTinhId(), searchTerms.getQuanHuyenId(),searchTerms.getPhuongXaId(),
						searchTerms.getTuNgayToaAn(),
						searchTerms.getDenNgayToaAn(),TrangThaiTienSuEnum.DANG_THI_HANH_AN.getValue(), -1,
						-1, null);
		for (DS_NguoiTienSuDTO item : canXoaAnTichs){
			List<TienSu> tienSuDangThiHanhAns = DoiTuongTienSuLocalServiceUtil.layDanhSachTienSuDangThiHanhAnCuaDoiTuong(item.getSoDinhDanhCaNhan());
			
			%>
			<li style="  padding-bottom: 11px;">
				<div  class="main-line">					
					<div class="check main-item  aui-w3 "><input type="checkbox" name="<portlet:namespace/>soDinhDanhCaNhanList"  value="<%=item.getSoDinhDanhCaNhan() %>" /></div>
					<div class="main-item  aui-w20"><%=item.getSoDinhDanhCaNhan() %></div>
					<div class="main-item  aui-w20"><%=item.getHoTen() %></div>
					<div class="main-item  aui-w12 ">
					<%=Validator.isNotNull(item.getNgaySinh()) ? dateFormat.format(item.getNgaySinh()) :""%>
					</div>
					<div class="main-item aui-w45"><%=item.getDiaChi() %></div>
				</div>
				<ul>
					<%
						for (TienSu ts : tienSuDangThiHanhAns){
					%>
					<li>
						<div class="child-line">
							<div class="check-child " style="width:7px;padding-top: 1px"><input name="<portlet:namespace/>tienSuIdList" type="checkbox" value="<%=ts.getTienSuId() %>" /></div>
							<div class="main-item aui-w97" style="padding-top: 1px">
							 <liferay-ui:message key="ban-an-so" />: <%=ts.getBanAnSo() %>,<liferay-ui:message key="ngay-tien-su" />: <%=Validator.isNotNull(ts.getNgayToaAn()) ? dateFormat.format(ts.getNgayToaAn()):"" %>, <liferay-ui:message key="toi-danh" />:  <%=ts.getToiDanh() %></div>
							
							
						</div>
											
					</li>
					<%} %>
				</ul>
			</li>
			<%-- <div class="main-line">
				<div class="check main-item  aui-w3 "><input type="checkbox" /></div>
				<div class="main-item  aui-w20"><%=item.getSoDinhDanhCaNhan() %></div>
				<div class="main-item  aui-w20"><%=item.getHoTen() %></div>
				<div class="main-item  aui-w8 ">
				<%=Validator.isNotNull(item.getNgaySinh()) ? dateFormat.format(item.getNgaySinh()) :""%>
				</div>
				<div class="main-item aui-w49"><%=item.getDiaChi() %></div>
			</div> --%>
			<%-- <%
			for (TienSu ts : tienSuDangThiHanhAns){
				%>
				<div class="child-line">
				<div class="check-child"><input type="checkbox" /></div>
				<div class="child-item"><%=ts.getBanAnSo() %></div>
				<div class="child-item"><%=ts.getToaAnXuPhat() %></div>
				
				<div class="child-item">
				<%=Validator.isNotNull(ts.getNgayToaAn()) ? dateFormat.format(ts.getNgayToaAn()) :""%>
				</div>
				<div class="child-item"><%=ts.getToiDanh() %></div>
				
				<div class="main-item"><%=item.getHinhPhat() %></div>
			</div>
			<%}indx++;
			%> --%>
			
		<%}
	%>
	</ul>
	</div>
<%-- <liferay-ui:search-container id="xoaAnTichBaoCaoSearchContainer" rowChecker="<%=rowChecker %>" emptyResultsMessage="no-web-content-were-found"  searchContainer="<%=dsTienSuSearch %>">
	<liferay-ui:search-container-results>
		<%
			results = 
			DoiTuongTienSuLocalServiceUtil.findDsTienSuByVungDiaLy_ThoiGianToaAn
					(searchTerms.getTinhId(), searchTerms.getQuanHuyenId(),searchTerms.getPhuongXaId(),
							searchTerms.getTuNgayToaAn(),
							searchTerms.getDenNgayToaAn(),TrangThaiTienSuEnum.DANG_THI_HANH_AN.getValue(), searchContainer.getStart(),
							searchContainer.getEnd(), searchContainer.getOrderByComparator());

			total = totalRow;
			
		
			pageContext.setAttribute("results", results);
			pageContext.setAttribute("total", total);
		%>
	</liferay-ui:search-container-results>
	
	<%
		int start = dsTienSuSearch.getDelta() * (dsTienSuSearch.getCur() - 1);
	%>
	
	<liferay-ui:search-container-row className="qldc.pm.tiensu.dto.DS_NguoiTienSuDTO" keyProperty="soDinhDanhCaNhan" modelVar="nguoiCoTienSu">

	
		<liferay-ui:search-container-column-text cssClass="aui-w3 txt-center " name="stt" value="<%=String.valueOf(++start) %>" />
		<liferay-ui:search-container-column-text cssClass="aui-w15 " name="so-dinh-danh-ca-nhan" property="soDinhDanhCaNhan"  orderableProperty="soDinhDanhCaNhan" orderable="<%=true %>" />		
		<liferay-ui:search-container-column-text cssClass="aui-w15" name="ho-va-ten" property="hoTen"  orderableProperty="hoTen" orderable="<%=true %>" />
		<liferay-ui:search-container-column-text cssClass="aui-w8" name="ngay-sinh"  orderableProperty="ngaySinh" orderable="<%=true %>" >
		<%=Validator.isNotNull(nguoiCoTienSu.getNgaySinh()) ? dateFormat.format(nguoiCoTienSu.getNgaySinh()) :""%>
		</liferay-ui:search-container-column-text>
		<liferay-ui:search-container-column-text  name="dia-chi-thuong-tru" property="diaChi" />
		<liferay-ui:search-container-column-text cssClass="txt-center aui-w10"  name="so-lan-pham-toi" property="soLanPhamToi"/>
		<liferay-ui:search-container-column-text cssClass="aui-w10"  name="ngay-toa-an" >
		<%=Validator.isNotNull(nguoiCoTienSu.getNgayToaAnMoiNhat()) ? dateFormat.format(nguoiCoTienSu.getNgayToaAnMoiNhat()) :""%>
		</liferay-ui:search-container-column-text>

		
	</liferay-ui:search-container-row>

	<liferay-ui:search-iterator searchContainer="<%=dsTienSuSearch %>" />
</liferay-ui:search-container> --%>
<%

	} else {
		%>
		<h5><liferay-ui:message key="vui-long-chon-tieu-chi-lap-bao-cao" /></h5>
	<%}
%>
	</div>
<script type="text/javascript">
$(function() {
	 loadQuanHuyen('<portlet:namespace />noiThuongTruTinhId', '<portlet:namespace />noiThuongTruQuanHuyenId', '<portlet:namespace />noiThuongTruPhuongXaId', '<%=loadQuanHuyen.toString() %>','<%=noiThuongTruQuanHuyenIdSelected %>');
	 loadPhuongXa('<portlet:namespace/>noiThuongTruQuanHuyenId', '<portlet:namespace/>noiThuongTruPhuongXaId', '<%=loadPhuongXa.toString() %>', '<%=noiThuongTruPhuongXaIdSelected %>');
});


function timDanhSach(){
		$('#<portlet:namespace/>frm').removeAttr("target");
		$('#<portlet:namespace/>frm').removeAttr("action");
		$('#<portlet:namespace/>cmd').val('<%=TienSuConstants.TIM%>');
		$("#<portlet:namespace/>frm").attr('onsubmit', 'return true;');
		$("#<portlet:namespace/>frm").submit(); 
		
};
function taoBaoCao(){
	var selectList = $('[name="<portlet:namespace/>soDinhDanhCaNhanList"]');
	var selectedSDDCN = getSelectedItem(selectList);
	selectList = $('[name="<portlet:namespace/>tienSuIdList"]');
	var selectedTienSu = getSelectedItem(selectList);
	
	if (selectedSDDCN.length == 0){
		showPopupConfirm('<liferay-ui:message key="xin-vui-long-chon-truong-hop-can-xoa-an-tich-de-lap-bao-cao" />',['Ok'],function(index){
		});
	} else {
		$('#<portlet:namespace/>soDinhDanhCaNhans').val(selectedSDDCN.join(','));
		$('#<portlet:namespace/>tienSuIds').val(selectedTienSu.join(','));
		 $('#<portlet:namespace/>cmd').val('<%=TienSuConstants.IN%>');
		 $("#<portlet:namespace/>frm").attr('target', '_blank');
		 $("#<portlet:namespace/>frm").submit();
	}
	
};
</script>
</fieldset>

